package com.sinsoft.monitorDemo.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sinosoft.framework.core.common.utils.ajax.ResponseData;
import com.sinsoft.monitorDemo.dao.OracleInfoDao;
import com.sinsoft.monitorDemo.dao.ServerDao;
import com.sinsoft.monitorDemo.entity.OracleInfo;
import com.sinsoft.monitorDemo.entity.ServerInfo;


@Controller
@RequestMapping("/OracleInfo")
public class OracleInfoController {
	
	/**
	 * 得到最近一段时间的的Oracle运行信息
	 * @return
	 */
	@RequestMapping(value = "/getOracleInfoList", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData getTomcatInfoList(HttpServletRequest request){
		OracleInfoDao dao = new OracleInfoDao();
		String ip = request.getParameter("ip");
		List<OracleInfo> oracleInfos = dao.getOracleInfoList(ip);
		ResponseData responseData = new ResponseData(true, oracleInfos);
		return responseData;	
	}
	
	/**
	 * 得到最新的oracle信息
	 * @return
	 */
	@RequestMapping(value = "/getNewOracleInfo", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData getNewOracleInfo(HttpServletRequest request){
		OracleInfoDao dao = new OracleInfoDao();
		String ip = request.getParameter("ip");
		OracleInfo oracleInfo = dao.getNewOracleInfo(ip);
		ResponseData responseData = new ResponseData(true, oracleInfo);
		return responseData;	
	}
	
	/**
	 * 得到最新的表空间使用率信息
	 * @return
	 */
	@RequestMapping(value = "/getOracleTableUserInfo", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData getOracleTableUserInfo(){
		ServerDao serverDao = new ServerDao();
		OracleInfoDao dao = new OracleInfoDao();
		ServerInfo serverInfo = serverDao.getServerInfo("ORACLE");
		Map<String, Object> tablespaceUsedPer =  new HashMap<String, Object>();
		String ip = serverInfo.getIp();
		if(ip.contains(",")){
			String[] split = ip.split(",");
			long maxtime = dao.getNewOracleInfo(split[0]).getTime();
			int j = 0;
			for (int i = 1; i < split.length; i++) {
				long time = dao.getNewOracleInfo(split[i]).getTime();
				if(maxtime<time){
					maxtime = time;
					j++;
				}
			}
			tablespaceUsedPer = dao.getNewOracleInfo(split[j]).getTablespaceUsedPer();
		}else {
			tablespaceUsedPer = dao.getNewOracleInfo(ip).getTablespaceUsedPer();
		}
		ResponseData responseData = new ResponseData(true, tablespaceUsedPer);
		return responseData;	
	}
	
	@RequestMapping(value = "/getAllOracleInfo", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData getAllOracleInfo(HttpServletRequest request){
		OracleInfoDao dao = new OracleInfoDao();
		Map<String, OracleInfo > map = new HashMap<String, OracleInfo>();
		String ip = request.getParameter("ip");
		if (ip.contains(",")) {
			String[] split = ip.split(",");
			for (int i = 0; i < split.length; i++) {
				OracleInfo oracleInfo = dao.getNewOracleInfo(split[i]);
				map.put(split[i], oracleInfo);
			}
		}else {
			OracleInfo oracleInfo = dao.getNewOracleInfo(ip);
			map.put(ip, oracleInfo);
		}
		ResponseData responseData = new ResponseData(true, map);
		return responseData;	
	}
	
	@RequestMapping(value = "/getAllOracleInfoList", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData getAllOracleInfoList(HttpServletRequest request){
		OracleInfoDao dao = new OracleInfoDao();
		Map<String, Object> map = new HashMap<String, Object>();
		String ip = request.getParameter("ip");
		if (ip.contains(",")) {
			String[] split = ip.split(",");
			for (int i = 0; i < split.length; i++) {
				List<OracleInfo> oracleInfoList = dao.getOracleInfoList(split[i]);
				map.put(split[i], oracleInfoList);
			}
		}else {
			List<OracleInfo> oracleInfoList = dao.getOracleInfoList(ip);
			map.put(ip, oracleInfoList);
		}
		ResponseData responseData = new ResponseData(true, map);
		return responseData;	
	}
}
